package cn.wolfcode.trip.base.service.impl;

import cn.wolfcode.trip.base.domain.Region;
import cn.wolfcode.trip.base.domain.Ticket;
import cn.wolfcode.trip.base.mapper.RegionMapper;
import cn.wolfcode.trip.base.mapper.TicketMapper;
import cn.wolfcode.trip.base.query.TicketQueryObject;
import cn.wolfcode.trip.base.service.ITicketService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class TicketServiceImpl implements ITicketService {
    @Autowired
    private TicketMapper ticketMapper;
    @Autowired
    private RegionMapper regionMapper;

    @Override
    public PageInfo query(TicketQueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize());
        List<Ticket> list=ticketMapper.selectForList(qo);
        return new PageInfo<>(list);
    }

    @Override
    public Ticket get(Long id) {
        return ticketMapper.selectByPrimaryKey(id);
    }

    @Override
    public void save(Ticket ticket) {
        ticketMapper.insert(ticket);
        Region region=new Region();
        region.setName(ticket.getName());
        region.setIsScenic(true);
        regionMapper.insert(region);

    }

    @Override
    public void update(Ticket ticket) {
        ticketMapper.updateByPrimaryKey(ticket);
    }

    @Override
    public List<Ticket> listAll() {
        return ticketMapper.selectAll();
    }
}
